﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using Telerik.Web.UI;
using KPIS.GERP.GIMS.MODEL;
using KPIS.GERP.GIMS.DAL ;
namespace KPIS.GERP.GIMS.APP.APP_UI.Form
{
    public partial class ICM_Master_Object_Type : System.Web.UI.Page
    {
        Common.Main main = new Common.Main();
        D_IcmMasObjectType d_obj_type = new D_IcmMasObjectType();
        M_IcmMasObjectType m_obj_type = new M_IcmMasObjectType();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UpdateUI1("Load");
                LoadGrid();
            }
        }
        protected void AddNew_Click(object sender, EventArgs e)
        {
            UpdateUI1("Insert");
            txtObjectTypeCode.Focus();
        }
        protected void AddNewSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (ValidateCheck())
                {
                    UpdateUI1("Save");
                    if (hstatus.Text != "Edit")
                    {
                        m_obj_type.icm_obj_type_code = txtObjectTypeCode.Text;
                        m_obj_type.icm_obj_type_name = txtObjectTypeName.Text;
                        m_obj_type.record_status = "U";
                        m_obj_type.created_by = main.UserID;
                        m_obj_type.created_when = DateTime.Now;
                        d_obj_type.Insert_Icm_Object_Type(m_obj_type);
                        LoadGrid();
                        RadAjaxManager1.Alert("บันทึกเรียบร้อยแล้ว");
                    }
                    else if (hstatus.Text == "Edit")
                    {
                        m_obj_type.icm_obj_type_seq = Convert.ToInt32(hseq.Text);
                        m_obj_type.icm_obj_type_code = txtObjectTypeCode.Text;
                        m_obj_type.icm_obj_type_name = txtObjectTypeName.Text;
                        m_obj_type.record_status = "U";
                        m_obj_type.updated_by = main.UserID;
                        m_obj_type.updated_when = DateTime.Now;
                        d_obj_type.Update_Icm_Object_Type(m_obj_type);
                        LoadGrid();
                        RadAjaxManager1.Alert("แก้ไขข้อมูลเรียบร้อย");
                    }
                    hstatus.Text = "";
                }
            }
            catch (Exception ex)
            {
                RadAjaxManager1.Alert("เกิดข้อผิดพลาดระหว่างบันทึกข้อมูล");
            }

        }
        protected void Cancel_Click(object sender, EventArgs e)
        {
            UpdateUI1("Cancel");
            ClearObj();
        }
        protected void grdObjectType_NeedDataSource(object sender ,GridNeedDataSourceEventArgs e)
        {
            LoadGrid();
        }
        protected void grdObjectType_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "DataEdit")
            {
                hstatus.Text = "Edit";
                M_IcmMasObjectType info = d_obj_type.GetListFillBySeq(main.ConvertNullInt(grdObjectType.Items[e.Item.ItemIndex]["icm_obj_type_seq"].Text));
                hseq.Text = info.icm_obj_type_seq.ToString();
                txtObjectTypeCode.Text = info.icm_obj_type_code.ToString();
                txtObjectTypeName.Text = info.icm_obj_type_name.ToString();
                UpdateUI1("Update");
                txtObjectTypeCode.Focus();
            }
            else if (e.CommandName == "DataDelete")
            {
                M_IcmMasObjectType info = d_obj_type.GetListFillBySeq(main.ConvertNullInt(grdObjectType.Items[e.Item.ItemIndex]["icm_obj_type_seq"].Text));
                m_obj_type.icm_obj_type_seq = info.icm_obj_type_seq;
                m_obj_type.icm_obj_type_code = info.icm_obj_type_code.ToString();
                m_obj_type.icm_obj_type_name = info.icm_obj_type_name.ToString();
                m_obj_type.record_status = "D";
                m_obj_type.updated_by = main.UserID;
                m_obj_type.updated_when = DateTime.Now;
                d_obj_type.Update_Icm_Object_Type(m_obj_type);
                ClearObj();
                LoadGrid();
                RadAjaxManager1.Alert("ลบข้อมูลเรียบร้อย");
            }
        }
        private void LoadGrid()
        {
            grdObjectType.DataSource = d_obj_type.GetList();
            grdObjectType.DataBind();
        }
        private void EnableObj(bool pFlag)
        {
            txtObjectTypeCode.Enabled = pFlag;
            txtObjectTypeName.Enabled = pFlag;
        }
        private void ClearObj()
        {
            txtObjectTypeCode.Text = "";
            txtObjectTypeName.Text = "";
            hstatus.Text = "";
        }
        private bool ValidateCheck()
        {
            if (txtObjectTypeCode.Text == "")
            {
                RadAjaxManager1.Alert("กรุณากรอกรหัส");
                txtObjectTypeCode.Focus();
                return false;
            }
            if (txtObjectTypeCode.Text.Length > 2)
            {
                RadAjaxManager1.Alert("ตัวอักษรที่กรอกเกิน 2 ตัว กรุณากรอกใหม่");
                txtObjectTypeCode.Focus();
                return false;
            }
            if (txtObjectTypeName.Text == "")
            {
                RadAjaxManager1.Alert("กรุณากรอกชื่อประเภทวัสดุ (ที่มีการขึ้นทะเบียนรหัส)ด้วย");
                txtObjectTypeName.Focus();
                return false;
            }
            if (txtObjectTypeName.Text.Length > 150)
            {
                RadAjaxManager1.Alert("ตัวอักษรเกิน 150 ตัวอักษร");
                txtObjectTypeName.Focus();
                return false;
            }
            return true;
        }
        private void UpdateUI1(string strCommand)
        {
            switch (strCommand)
            {
                case "Load":
                    EnableObj(false);
                    ClearObj();
                    AddNew.Enabled = true;
                    AddNewSave.Enabled = false;
                    Cancel.Enabled = false;
                    break;
                case "Insert":
                    EnableObj(true);
                    ClearObj();
                    AddNew.Enabled = false;
                    AddNewSave.Enabled = true;
                    Cancel.Enabled = true;
                    break;
                case "Update":
                    EnableObj(true);
                    AddNew.Enabled = false;
                    AddNewSave.Enabled = true;
                    Cancel.Enabled = true;
                    break;
                case "Save":
                    EnableObj(false);
                    AddNew.Enabled = true;
                    AddNewSave.Enabled = false;
                    Cancel.Enabled = false;
                    break;
                case "Cancel":
                    EnableObj(false);
                    AddNew.Enabled = true;
                    AddNewSave.Enabled = false;
                    Cancel.Enabled = false;
                    break;
            }
        }
    }
}
